package com.lagou.homework2;

import java.util.Arrays;

public class ArraysTest {

    public static void main(String[] args) {

        int[] arr = null;
        System.out.println(toString(arr));
        int[] arr1 = new int[0];
        System.out.println(toString(arr1));
        int[] arr2 = {1, 2, 3, 4, 5};
        System.out.println(Arrays.toString(arr2));
    }

    public static String toString(int[] arr) {
        if (arr == null) {
            return "null";
        }
        int iMax = arr.length - 1;
        if (iMax == -1) {
            return "[]";
        }
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < arr.length; i++) {
            //sb.append(arr[i]);
            if (i == iMax) {
                return sb.append(arr[i]).append("]").toString();
            } else  {
                return sb.append(arr[i]).append(", ").toString();
            }

        }
        return sb.toString();
    }
}
